<?php
class Db
{
    private $host;
    private $dbh;
    private $dbname;
    private $name;
    private $pwd;
    public function __construct($host,$dbname,$name,$pwd)
    {
        $this->host=$host;
        $this->dbname=$dbname;
        $this->name=$name;
        $this->pwd=$pwd;
        $this->dbh=new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->name,$this->pwd);
    }
    public function getOne($table,$where='1'){
        if(empty($table)){
            return false;
        }
        $sql = "select * from $table where $where";
        $data = $this->dbh->query($sql);
        return $data->fetch(2);
    }
    public function getAll($table,$where){
        $sql = "select * from $table where $where";
        $data = $this->dbh->query($sql);
        return $data->fetchall(2);
    }
    public function addAll($table,$arr){
        if(count($arr)==0){
            return false;
        }
        if(empty($arr)){
            return false;
        }
        $k='';
        $v='';
        foreach($arr as $key =>$value){
            $k.=",`$key`";
            $v.=",'$value'";
        }
        $k=substr($k,1);
        $v=substr($v,1);
        $sql = "insert into $table($k) value ($v)";
        $data = $this->dbh->exec($sql);
        return $sql;
//        return $data->fetch(2);
    }
    public function delAll($table,$where){
        $sql = "delete from $table where $where";
        $res = $this->dbh->exec($sql);
        return $res;
    }
    public function updeOne($table,$set,$where){
        $str='';
        foreach($set as $key =>$value){
            $str.=",`$key`='$value'";
        }
        $str=substr($str,1);
        $sql = "update $table set $str where $where";
        $res = $this->dbh->exec($sql);
        return $res;
    }
}
$obj=new Db("127.0.0.1","week","root","zzf");
//$data = $obj->getOne('news','news_id=5'); //查询一条数据
//$data = $obj->getAll('news','news_id in (5,6,7,8,9,10)'); //查询所有数据
$arr = array("news_author"=>"sdafsd ","news_content"=>"afdsafds");//修改后数据
$data = $obj->addAll('news',$arr); //添加数据
//$data = $obj->delAll('news','news_id=5'); //删除数据
//$arr = array("news_author"=>"sdafsd ","news_content"=>"afdsafds");//修改后数据
//$data = $obj->updeOne('news',$arr,'news_id=10');//修改数据
print_r($data);
?>